﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace Wallpaper.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddRoleMenuTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_carousels_create_at",
                table: "carousels");

            migrationBuilder.DropColumn(
                name: "create_at",
                table: "carousels");

            migrationBuilder.RenameColumn(
                name: "sort_order",
                table: "carousels",
                newName: "SortOrder");

            migrationBuilder.RenameColumn(
                name: "is_active",
                table: "carousels",
                newName: "IsActive");

            migrationBuilder.RenameColumn(
                name: "carousel_url",
                table: "carousels",
                newName: "CarouselUrl");

            migrationBuilder.RenameColumn(
                name: "update_at",
                table: "carousels",
                newName: "UpdatedAt");

            migrationBuilder.RenameIndex(
                name: "IX_carousels_active_sort",
                table: "carousels",
                newName: "IX_Carousels_Active_Sort");

            migrationBuilder.AlterColumn<string>(
                name: "AvatarUrl",
                table: "users",
                type: "nvarchar(200)",
                maxLength: 200,
                nullable: true,
                comment: "头像图片URL地址",
                oldClrType: typeof(string),
                oldType: "nvarchar(50)",
                oldMaxLength: 50,
                oldNullable: true,
                oldComment: "头像图片URL地址");

            migrationBuilder.AddColumn<DateTime>(
                name: "CreatedAt",
                table: "carousels",
                type: "datetime2",
                nullable: false,
                defaultValueSql: "GETDATE()",
                comment: "创建时间");

            migrationBuilder.AddColumn<string>(
                name: "RoleId",
                table: "admins",
                type: "nvarchar(36)",
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateTable(
                name: "menus",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "菜单主键ID"),
                    Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "菜单名称"),
                    Path = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "路由路径"),
                    Icon = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "菜单图标"),
                    Component = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "组件路径"),
                    Sort = table.Column<int>(type: "int", nullable: false, defaultValue: 0, comment: "排序号"),
                    ParentId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: true, comment: "父级菜单ID"),
                    IsEnabled = table.Column<bool>(type: "bit", nullable: false, defaultValue: true, comment: "启用状态"),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "软删除标志"),
                    CreateAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()", comment: "创建时间"),
                    UpdateAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_menus", x => x.Id);
                    table.ForeignKey(
                        name: "FK_menus_menus_ParentId",
                        column: x => x.ParentId,
                        principalTable: "menus",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "roles",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "角色主键ID"),
                    RoleName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    RoleCode = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
                    Description = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    IsEnabled = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    CreateAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
                    UpdateAt = table.Column<DateTime>(type: "datetime2", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_roles", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "RoleMenu",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    RoleId = table.Column<string>(type: "nvarchar(36)", nullable: false),
                    MenuId = table.Column<string>(type: "nvarchar(36)", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RoleMenu", x => x.Id);
                    table.ForeignKey(
                        name: "FK_RoleMenu_menus_MenuId",
                        column: x => x.MenuId,
                        principalTable: "menus",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RoleMenu_roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "menus",
                columns: new[] { "Id", "Component", "CreateAt", "Icon", "IsEnabled", "Name", "ParentId", "Path", "Sort", "UpdateAt" },
                values: new object[,]
                {
                    { "69eab353-82e5-4510-8289-b061029d673c", "dashboard/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4516), "House", true, "仪表盘", null, "/dashboard", 1, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "88430d82-5397-4808-b2c3-50e0a72b6636", null, new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4575), "Tools", true, "系统工具", null, null, 5, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "8bf4d815-5bc6-4d41-babe-9d9a27a4e5eb", null, new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4554), "Document", true, "内容管理", null, null, 3, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "b3ad11e7-9101-46bd-bcf7-7b54fc0a1d97", null, new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4541), "Setting", true, "系统管理", null, null, 2, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "e4052b6f-ec9b-4712-8e2f-b20b07760c29", null, new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4568), "DataLine", true, "数据统计", null, null, 4, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) }
                });

            migrationBuilder.InsertData(
                table: "roles",
                columns: new[] { "Id", "CreateAt", "Description", "IsEnabled", "RoleCode", "RoleName", "UpdateAt" },
                values: new object[,]
                {
                    { "8308ca55-8e2e-4d89-8bc7-b1dcccf9b41b", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(8557), "负责审核和管理的壁纸的角色", true, "wallpaper_auditor", "壁纸审核员", new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "e213eaff-1038-438f-aeba-d0eb2445e557", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(8541), "系统超级管理员，拥有所有权限", true, "super_admin", "超级管理员", new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "ea3815c4-72e6-40e4-bc8c-00d5a71ce32c", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(8554), "系统管理员", true, "admin", "管理员", new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) }
                });

            migrationBuilder.InsertData(
                table: "menus",
                columns: new[] { "Id", "Component", "CreateAt", "Icon", "IsEnabled", "Name", "ParentId", "Path", "Sort", "UpdateAt" },
                values: new object[,]
                {
                    { "0456f792-07e9-433b-a0c0-63348eb800eb", "content/category/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4562), "Collection", true, "分类管理", "8bf4d815-5bc6-4d41-babe-9d9a27a4e5eb", "/content/category", 2, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "38b9332e-c128-475e-ab7c-926ab199bffd", "system/user/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4545), "User", true, "用户管理", "b3ad11e7-9101-46bd-bcf7-7b54fc0a1d97", "/system/user", 1, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "51f6f806-d97e-4dc9-92fb-ffee6b0807a8", "tools/monitor/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4581), "Monitor", true, "系统监控", "88430d82-5397-4808-b2c3-50e0a72b6636", "/tools/monitor", 2, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "549916d1-8207-456b-b553-f58fe4af1f92", "system/permission/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4551), "Lock", true, "权限管理", "b3ad11e7-9101-46bd-bcf7-7b54fc0a1d97", "/system/permission", 3, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "56db9ce0-5957-4f75-82b1-84dda20268e6", "tools/log/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4578), "List", true, "系统日志", "88430d82-5397-4808-b2c3-50e0a72b6636", "/tools/log", 1, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "67c9ebc4-6ddd-48e9-bf1f-9ab04ac8ec12", "statistics/sales/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4574), "Wallet", true, "销售统计", "e4052b6f-ec9b-4712-8e2f-b20b07760c29", "/statistics/sales", 2, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "73f06219-6e8b-4bf2-bd62-0b0d30eba629", "system/role/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4548), "UserFilled", true, "角色管理", "b3ad11e7-9101-46bd-bcf7-7b54fc0a1d97", "/system/role", 2, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "7408ec9d-9ee9-4b88-b6f7-f01cbb9501e5", "content/tag/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4565), "PriceTag", true, "标签管理", "8bf4d815-5bc6-4d41-babe-9d9a27a4e5eb", "/content/tag", 3, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "cdf941ff-1227-42a2-864c-4d7592deb576", "statistics/visit/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4571), "View", true, "访问统计", "e4052b6f-ec9b-4712-8e2f-b20b07760c29", "/statistics/visit", 1, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "dffcab4b-4ca4-4ec9-afbe-e1062a809010", "content/article/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4559), "Reading", true, "文章列表", "8bf4d815-5bc6-4d41-babe-9d9a27a4e5eb", "/content/article", 1, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) },
                    { "e4ae7e8d-cec1-498c-859d-8e98097a995c", "tools/backup/index", new DateTime(2025, 10, 9, 17, 41, 9, 332, DateTimeKind.Local).AddTicks(4583), "DataAnalysis", true, "数据备份", "88430d82-5397-4808-b2c3-50e0a72b6636", "/tools/backup", 3, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified) }
                });

            migrationBuilder.CreateIndex(
                name: "IX_Carousels_CreateAt",
                table: "carousels",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_admins_RoleId",
                table: "admins",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_Menu_Enabled_Sort",
                table: "menus",
                columns: new[] { "IsEnabled", "Sort" });

            migrationBuilder.CreateIndex(
                name: "IX_Menu_ParentId",
                table: "menus",
                column: "ParentId");

            migrationBuilder.CreateIndex(
                name: "IX_Menu_Path",
                table: "menus",
                column: "Path");

            migrationBuilder.CreateIndex(
                name: "IX_RoleMenu_MenuId",
                table: "RoleMenu",
                column: "MenuId");

            migrationBuilder.CreateIndex(
                name: "IX_RoleMenu_RoleId",
                table: "RoleMenu",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_Role_RoleCode",
                table: "roles",
                column: "RoleCode",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Role_RoleName",
                table: "roles",
                column: "RoleName");

            migrationBuilder.AddForeignKey(
                name: "FK_admins_roles_RoleId",
                table: "admins",
                column: "RoleId",
                principalTable: "roles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_admins_roles_RoleId",
                table: "admins");

            migrationBuilder.DropTable(
                name: "RoleMenu");

            migrationBuilder.DropTable(
                name: "menus");

            migrationBuilder.DropTable(
                name: "roles");

            migrationBuilder.DropIndex(
                name: "IX_Carousels_CreateAt",
                table: "carousels");

            migrationBuilder.DropIndex(
                name: "IX_admins_RoleId",
                table: "admins");

            migrationBuilder.DropColumn(
                name: "CreatedAt",
                table: "carousels");

            migrationBuilder.DropColumn(
                name: "RoleId",
                table: "admins");

            migrationBuilder.RenameColumn(
                name: "SortOrder",
                table: "carousels",
                newName: "sort_order");

            migrationBuilder.RenameColumn(
                name: "IsActive",
                table: "carousels",
                newName: "is_active");

            migrationBuilder.RenameColumn(
                name: "CarouselUrl",
                table: "carousels",
                newName: "carousel_url");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "carousels",
                newName: "update_at");

            migrationBuilder.RenameIndex(
                name: "IX_Carousels_Active_Sort",
                table: "carousels",
                newName: "IX_carousels_active_sort");

            migrationBuilder.AlterColumn<string>(
                name: "AvatarUrl",
                table: "users",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: true,
                comment: "头像图片URL地址",
                oldClrType: typeof(string),
                oldType: "nvarchar(200)",
                oldMaxLength: 200,
                oldNullable: true,
                oldComment: "头像图片URL地址");

            migrationBuilder.AddColumn<DateTime>(
                name: "create_at",
                table: "carousels",
                type: "datetime2",
                nullable: false,
                defaultValueSql: "CURRENT_TIMESTAMP",
                comment: "创建时间");

            migrationBuilder.CreateIndex(
                name: "IX_carousels_create_at",
                table: "carousels",
                column: "create_at");
        }
    }
}
